package com.cloud.form.api.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.form.api.bean.WfActDef;
import com.cloud.form.api.bean.WfActPart;
import com.cloud.form.api.bean.WfProcDef;
import com.cloud.form.common.result.QkIcon;
import com.cloud.form.common.result.QkTreeNode;
import com.cloud.form.common.result.Quokka;
import com.cloud.form.common.utils.TreeHelper;
import com.cloud.form.interfaces.FeignClientFreeflow;

@RestController
@RequestMapping("/form/api/freeflow")
public class FormFreeflowController {

	@Autowired
	private FeignClientFreeflow feignClientFreeflow;

	@GetMapping("listbizprocs")
	public Quokka listBizProcs(Long bizId) {
		if(null == bizId)bizId = 0L;
		return new Quokka(true, feignClientFreeflow.listProcDef(bizId));
	}

	@GetMapping("listbizproctree")
	public Quokka listBizProcTree(Long bizId) {
		List<WfProcDef> procs = feignClientFreeflow.listProcDef(bizId);
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>();
		List<Long> includeProc = new ArrayList<Long>();
		for (WfProcDef def : procs) {
			if (!includeProc.contains(def.getBizId())) {
				QkTreeNode bizNode = new QkTreeNode("BIZ_" + def.getBizId(), def.getBizName());
				bizNode.setSelectable(false);
				bizNode.setRootFlag(true);
				bizNode.setIcon(QkIcon.FA_LAPTOP);
				bizNode.setExpanded(true);
				includeProc.add(def.getBizId());
				nodes.add(bizNode);
			}
			QkTreeNode procNode = new QkTreeNode("PROC_" + def.getProcDefId(), def.getProcDefName());
			procNode.setSelectable(false);
			procNode.setIcon(QkIcon.FA_SHARE_ALT);
			procNode.setLeaf(true);
			procNode.setSelectable(true);
			procNode.setSeniorId("BIZ_" + def.getBizId());
			nodes.add(procNode);
		}
		return new Quokka(true, TreeHelper.buildList2Tree(nodes));
	}

	@GetMapping("loadactpartstree")
	public Quokka loadActPartsTree(Long procDefId) {
		List<WfActDef> acts = feignClientFreeflow.loadActDefByProc(procDefId);
		List<QkTreeNode> node = new ArrayList<QkTreeNode>();
		for (WfActDef act : acts) {
			QkTreeNode actNode = new QkTreeNode("ACT_" + act.getActDefId(), act.getActDefName());
			actNode.setRootFlag(true);
			actNode.setSelectable(true);
			actNode.setIcon(QkIcon.FA_SKYATLAS);
			actNode.setExpanded(true);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("actDefId", act.getActDefId());
			data.put("partId", 0);
			actNode.setData(data);
			node.add(actNode);
			List<WfActPart> parts = act.getParts();
			for (WfActPart part : parts) {
				QkTreeNode partNode = new QkTreeNode("PART_" + part.getPartId(), part.getPartName());
				partNode.setSelectable(true);
				partNode.setLeaf(true);
				partNode.setIcon(QkIcon.FA_SHARE_ALT);
				partNode.setSeniorId("ACT_" + act.getActDefId());
				Map<String, Object> partData = new HashMap<String, Object>();
				partData.put("actDefId", act.getActDefId());
				partData.put("partId", part.getPartId());
				partNode.setData(partData);
				node.add(partNode);
			}
		}
		return new Quokka(true, TreeHelper.buildList2Tree(node));
	}

}
